<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="公司名称">
              <a-input placeholder="请输入公司名称" v-model="queryParam.company"></a-input>
            </a-form-item>
          </a-col>

          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="openModal('add', {})" type="primary" icon="plus">新增</a-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete" />删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down"/></a-button>
      </a-dropdown>
    </div>

    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <a @click="openModal('edit', record)">编辑</a>
          <a-divider type="vertical" />
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a style="color: red;">删除</a>
          </a-popconfirm>
        </span>
      </a-table>
    </div>

    <a-modal
      :visible="visible"
      :width="500"
      :title="curRowType === 'add' ? '新增用户需求' : '编辑用户需求'"
      :confirmLoading="submitLoad"
      destroyOnClose
      @cancel="handleCancel"
      @ok="handleOperate"
    >
      <a-form-model ref="ruleForm" :model="formData">
        <a-row>
          <a-col :span="24">
            <a-form-model-item
              label="公司名称"
              prop="company"
              :labelCol="{ span: 4 }"
              :wrapperCol="{ span: 18, offset: 1 }"
              labelInValue
              :rules="{
                required: true,
                message: '请输入公司名称',
                trigget: 'blur'
              }"
            >
              <a-input style="width: 100%;" v-model="formData.company" placeholder="请输入公司名称" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="行业类型"
              prop="industryType"
              :labelCol="{ span: 4 }"
              :wrapperCol="{ span: 18, offset: 1 }"
              :rules="[
                {
                  required: true,
                  message: '请选择行业类型',
                  trigget: 'blur'
                },
                {
                  pattern: /^1\${10}&/,
                  message: '请输入正确手机号码',
                  trigget: 'blur'
                }
              ]"
            >
              <!-- <a-input style="width: 100%;" v-model="formData.industryType" placeholder="请选择行业类型" /> -->
              <!-- <a-select v-model="formData.companyId" placeholder="请选择行业类型" showSearch>
                <a-select-option v-for="item in companyList" :key="item.industryType">{{ item.customName }}</a-select-option>
              </a-select> -->
              <j-dict-select-tag v-model="formData.industryType" placeholder="请选择行业类型" dictCode="IndustryType" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="手机号码"
              prop="phoneNumber"
              :labelCol="{ span: 4 }"
              :wrapperCol="{ span: 18, offset: 1 }"
              labelInValue
              :rules="{
                required: true,
                message: '请输入手机号码',
                trigget: 'blur'
              }"
            >
              <a-input style="width: 100%;" v-model="formData.phoneNumber" placeholder="请输入手机号码" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="需求备注"
              prop="requirement"
              :labelCol="{ span: 4 }"
              :wrapperCol="{ span: 18, offset: 1 }"
            >
              <a-input type="textarea" v-model="formData.requirement" placeholder="请输入需求备注" :maxLength="500" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { apiAddUserDemand, apiEditUserDemand } from '@/api/userDemand.js'
import { filterDictTextByCache } from '@/components/dict/JDictSelectUtil'

export default {
  name: 'PolicyDoc',
  mixins: [JeecgListMixin, mixinDevice],
  data() {
    return {
      description: '政策文档管理',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '公司名称',
          align: 'center',
          dataIndex: 'company'
        },
        {
          title: '行业类型',
          align: 'center',
          customRender: cur => {
            //字典值翻译通用方法
            return filterDictTextByCache('IndustryType', cur.industryType)
          }
        },
        {
          title: '手机号码',
          align: 'center',
          dataIndex: 'phoneNumber'
        },
        {
          title: '需求描述',
          align: 'center',
          dataIndex: 'requirement'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/userRequirementController/list',
        delete: '/userRequirementController/delete',
        deleteBatch: '/userRequirementController/deleteBatch'
      },
      curType: '',
      visible: false,
      modalTitle: '新增用户需求',
      submitLoad: false,
      formData: {
        company: undefined,
        industryType: undefined,
        phoneNumber: undefined,
        requirement: undefined
      },
      curRow: {},
      curRowType: 'add',
      companyList: []
    }
  },
  created() {},
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
    userType() {
      return this.$store.state.user.info.userType
    },
    actionUrl() {
      return process.env.VUE_APP_API_BASE_URL + '/api/file/upload'
    }
  },
  methods: {
    handleQuotation(record) {
      this.curType = record.userType
      this.customId = record.id
      this.curStatus = 'operate'
      this.packagePriceId = record.packagePriceId
      this.$refs.QuotationModalRef.visible = true
    },
    openModal(type, row) {
      this.curRowType = type
      this.curRow = row
      this.visible = true
      if (type !== 'add') {
        this.formData = Object.assign({}, row)
      }
    },
    async handleOperate() {
      const apiFun = this.curRowType === 'add' ? apiAddUserDemand : apiEditUserDemand
      await this.$refs.ruleForm.validate()
      // let name = this.companyList.find(item => item.id === this.formData.companyId).customName
      this.submitLoad = true
      console.log(13123)

      const { code, message } = await apiFun({
        ...this.formData
      })
      this.submitLoad = false
      if (code === 200) {
        this.handleCancel()
        this.searchQuery()
        this.$message.success('操作成功！')
      } else {
        this.$message.success(message)
      }
    },
    handleCancel() {
      this.visible = false
      this.formData = {
        company: undefined,
        industryType: undefined,
        phoneNumber: undefined,
        requirement: undefined
      }
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
.file_item {
  margin: 10px 0;
  padding: 5px;
  display: flex;
  justify-content: space-between;
  align-content: center;
  font-size: 14px;
  border-radius: 2px;
  cursor: pointer;
}
.file_item:hover {
  background-color: #e6f7ff;
  color: #40a9ff;
}
</style>
